package com.lxf.clouduser.controller;

import com.alibaba.fastjson.JSONObject;
import com.lxf.clouduser.service.UserMsgService;
import com.lxf.clouduser.utils.SendNettyMsgUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * @author 罗兴发
 * @data 2025/6/18 15:34
 * @description：用户消息-控制层
 */
@RestController
@CrossOrigin  //跨域
@RequestMapping("/v1")
@Slf4j
public class MsgController {
    @Resource
    UserMsgService userMsgService;

    @Resource
    private SendNettyMsgUtils sendNettyMsgUtils;

    /**
     * 发送消息通知
     * @param json
     */
    @PostMapping("/sendPrivateMsg")
    public void sendPrivateMsg(@RequestBody JSONObject json){
        try {
            log.info("进入发送消息接口");
            String userId = json.getString("user_id");
            String fUserId = json.getString("f_user_id");
            String msg = json.getString("msg");
            Integer msgType = json.getInteger("msg_type");
            int i = sendNettyMsgUtils.pushMsgToUser(msgType,fUserId,userId, msg);
            if (i==1){
                log.info("消息发送接口请求完成");
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    /**
     * 发送消息通知
     * @param json
     */
    @PostMapping("/sendPublicMsg")
    public void sendPublicMsg(@RequestBody JSONObject json){
        try {
            log.info("进入发送公告接口");
            String msg = json.getString("msg");
            int i = sendNettyMsgUtils.pushToAll( msg);
            if (i==1){
                log.info("消息发送公告接口请求完成");
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    /**
     * 根据消息ID查询用户消息
     * @param json
     * @param request
     * @return
     */
    @PostMapping("/getUserMsgById")
    public String getUserMsgById(@RequestBody JSONObject json, HttpServletRequest request){
        return userMsgService.getUserMsgById(json,request);
    }

    /**
     * 根据消息id更新消息
     * @param json
     * @param request
     * @return
     */
    @PostMapping("/updateMsgStatusById")
    public String updateMsgStatusById(@RequestBody JSONObject json, HttpServletRequest request){
        return userMsgService.updateMsgStatusById(json,request);
    }
}
